package protocol.transactions;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import android.content.Context;

import com.netease.http.THttpRequest;
import com.netease.log.NTLog;
import com.netease.pkgRelated.XoneConsts;
import com.netease.task.example.StringAsyncTransaction;


public class FeedBackTransaction extends StringAsyncTransaction {
    int mType;//参考XoneConsts.FEEDBACK_TYPE
    String mUser;
    String mTitle;
    String mContent;
    String mFileId;
    String mContact;
    Context mContext;

    public FeedBackTransaction(int type,String user,String title,String content,String fileId,String contact,Context context) {
        super(XoneBaseTransaction.TRANSACTION_TYPE_FEEDBACK);
        mType = type;
        mUser = user;
        mTitle = title;
        mContent = content;
        mFileId = fileId;
        mContact = contact; 
        mContext = context;
    }

    @Override
    protected void onTransactionError(int errCode, Object obj) {
        notifyError(errCode, obj);
    }

    @Override
    protected void onTransactionSuccess(int code, Object obj) {
        NTLog.e("FeedBackTransaction", "obj is" + obj);
        boolean res = false;
        if (obj != null && obj instanceof String) {
            String response = (String)obj;
            if(response.equalsIgnoreCase("true")){
                res = true;
            } 
        }
        if(res){
            notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS,null);
        } else {
            notifyError(XoneServiceCode.ERR_CODE_DATA_PARSE_EXCEPTION, null);
        }
    }

    @Override
    public void onTransact() {
        String pid = XoneProtocol.PRODUCT_ID;
        String fid = XoneProtocol.FEEDBACK_TYPE_ID_APP;
        if(mType == XoneConsts.FEEDBACK_TYPE.Info){
            fid = XoneProtocol.FEEDBACK_TYPE_ID_INFO;
        } else if(mType == XoneConsts.FEEDBACK_TYPE.Crash){
            pid = XoneProtocol.PRODUCT_ID_DEBUG;
            fid = XoneProtocol.FEEDBACK_TYPE_ID_CRASH;
        }
        THttpRequest request = XoneProtocol.getInstance().createFeedBackReqeust(pid,fid,mUser,mTitle,mContent,mFileId,mContact,mContext);
        sendRequest(request);
    }

}
